package user

import (
	"context"
	"go-zero-all-demos/apps/user/rpc/userservice"

	"go-zero-all-demos/apps/api/internal/svc"
	"go-zero-all-demos/apps/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddUserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddUserLogic {
	return &AddUserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddUserLogic) AddUser(req *types.UserInfo) (resp *types.UserID, err error) {
	user, err := l.svcCtx.UserRPC.AddUser(l.ctx, &userservice.UserInfo{
		Name:     req.Name,
		Password: req.Password,
	})
	if err != nil {
		return nil, err
	}
	return &types.UserID{Id: user.Id}, nil
}
